{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0",
   "metadata": {},
   "source": [
    "[![image](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/gee-community/geemap/blob/master/docs/workshops/Alaska_2024_Part2.ipynb)\n",
    "\n",
    "**Geospatial Cloud Computing with the GEE Python API - Part 2**\n",
    "\n",
    "-   Notebook: <https://geemap.org/workshops/Alaska_2024_Part2>\n",
    "-   Earth Engine: <https://earthengine.google.com>\n",
    "-   Geemap: <https://geemap.org>\n",
    "\n",
    "## Introduction\n",
    "\n",
    "This notebook contains the materials for the first part of the workshop **Geospatial Cloud Computing with the GEE Python API** at the University of Alaska Fairbanks.\n",
    "\n",
    "This workshop provides an introduction to cloud-based geospatial analysis using the Earth Engine Python API. Attendees will learn the basics of Earth Engine data types and how to visualize, analyze, and export Earth Engine data in a Jupyter environment with geemap. In addition, attendees will learn how to develop and deploy interactive Earth Engine web apps with Python. Through practical examples and hands-on exercises, attendees will enhance their learning experience. During each hands-on session, attendees will walk through Jupyter Notebook examples on Google Colab with the instructors. At the end of each session, they will complete a hands-on exercise to apply the knowledge they have learned.\n",
    "\n",
    "### Agenda\n",
    "\n",
    "The workshop is divided into three parts. The second part will cover the following topics:\n",
    "\n",
    "-   Processing of vector data (shapefiles, json, conversion from one format to another)\n",
    "-   Processing of raster data: extract pixel value, raster calculator, zonal statistics etc.\n",
    "-   Working with local geospatial data in Geemap\n",
    "-   Accessing Cloud Optimized GeoTIFF\n",
    "-   Exporting EE Image and Feature data\n",
    "-   Creating timelapse animations using Landsat or Sentinel 2 for Alaska\n",
    "-   Time series analysis: Forest cover change for a test site in Alaska (e.g. Bonanza Creek LTER or Caribou-Poker Creeks Research Watershed)\n",
    "\n",
    "### Prerequisites\n",
    "\n",
    "-   To use geemap and the Earth Engine Python API, you must [register](https://code.earthengine.google.com/register) for an Earth Engine account and follow the instructions [here](https://docs.google.com/document/d/1ZGSmrNm6_baqd8CHt33kIBWOlvkh-HLr46bODgJN1h0/edit?usp=sharing) to create a Cloud Project. Earth Engine is free for [noncommercial and research use](https://earthengine.google.com/noncommercial). To test whether you can use authenticate the Earth Engine Python API, please run [this notebook](https://colab.research.google.com/github/gee-community/geemap/blob/master/docs/notebooks/geemap_colab.ipynb) on Google Colab.\n",
    "\n",
    "## Technical requirements\n",
    "\n",
    "### Install packages\n",
    "\n",
    "```bash\n",
    "conda create -n gee python=3.11\n",
    "conda activate gee\n",
    "conda install -c conda-forge mamba\n",
    "mamba install -c conda-forge pygis\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# %pip install geemap pygis"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2",
   "metadata": {},
   "source": [
    "### Import libraries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3",
   "metadata": {},
   "outputs": [],
   "source": [
    "import ee\n",
    "import geemap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.ee_initialize()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5",
   "metadata": {},
   "source": [
    "## Processing of vector data\n",
    "\n",
    "### From GeoJSON"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6",
   "metadata": {},
   "outputs": [],
   "source": [
    "in_geojson = \"https://github.com/gee-community/geemap/blob/master/examples/data/countries.geojson\"\n",
    "m = geemap.Map()\n",
    "fc = geemap.geojson_to_ee(in_geojson)\n",
    "m.add_layer(fc.style(**{\"color\": \"ff0000\", \"fillColor\": \"00000000\"}), {}, \"Countries\")\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7",
   "metadata": {},
   "source": [
    "### From Shapefile"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8",
   "metadata": {},
   "outputs": [],
   "source": [
    "url = \"https://github.com/gee-community/geemap/blob/master/examples/data/countries.zip\"\n",
    "geemap.download_file(url, overwrite=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9",
   "metadata": {},
   "outputs": [],
   "source": [
    "in_shp = \"countries.shp\"\n",
    "fc = geemap.shp_to_ee(in_shp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "10",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = geemap.Map()\n",
    "m.add_layer(fc, {}, \"Countries\")\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "11",
   "metadata": {},
   "source": [
    "### From GeoDataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "12",
   "metadata": {},
   "outputs": [],
   "source": [
    "import geopandas as gpd\n",
    "\n",
    "gdf = gpd.read_file(in_shp)\n",
    "fc = geemap.gdf_to_ee(gdf)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "13",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = geemap.Map()\n",
    "m.add_layer(fc, {}, \"Countries\")\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "14",
   "metadata": {},
   "source": [
    "### To GeoJSON"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "15",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = geemap.Map()\n",
    "states = ee.FeatureCollection(\"TIGER/2018/States\")\n",
    "fc = states.filter(ee.Filter.eq(\"NAME\", \"Alaska\"))\n",
    "m.add_layer(fc, {}, \"Alaska\")\n",
    "m.center_object(fc, 4)\n",
    "m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "16",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.ee_to_geojson(fc, filename=\"Alaska.geojson\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "17",
   "metadata": {},
   "source": [
    "### To Shapefile"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "18",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.ee_to_shp(fc, filename=\"Alaska.shp\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "19",
   "metadata": {},
   "source": [
    "### To GeoDataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "20",
   "metadata": {},
   "outputs": [],
   "source": [
    "gdf = geemap.ee_to_gdf(fc)\n",
    "gdf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "21",
   "metadata": {},
   "outputs": [],
   "source": [
    "gdf.explore()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "22",
   "metadata": {},
   "source": [
    "### To DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "23",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = geemap.ee_to_df(fc)\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "24",
   "metadata": {},
   "source": [
    "### To CSV"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "25",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.ee_to_csv(fc, filename=\"Alaska.csv\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "26",
   "metadata": {},
   "source": [
    "## Processing of raster data\n",
    "\n",
    "### Extract pixel values\n",
    "\n",
    "#### Extracting values to points"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "27",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = geemap.Map(center=[40, -100], zoom=4)\n",
    "\n",
    "dem = ee.Image(\"USGS/SRTMGL1_003\")\n",
    "landsat7 = ee.Image(\"LANDSAT/LE7_TOA_5YEAR/1999_2003\")\n",
    "\n",
    "vis_params = {\n",
    "    \"min\": 0,\n",
    "    \"max\": 4000,\n",
    "    \"palette\": [\"006633\", \"E5FFCC\", \"662A00\", \"D8D8D8\", \"F5F5F5\"],\n",
    "}\n",
    "\n",
    "m.add_layer(\n",
    "    landsat7,\n",
    "    {\"bands\": [\"B4\", \"B3\", \"B2\"], \"min\": 20, \"max\": 200, \"gamma\": 2},\n",
    "    \"Landsat 7\",\n",
    ")\n",
    "m.add_layer(dem, vis_params, \"SRTM DEM\", True, 1)\n",
    "m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "28",
   "metadata": {},
   "outputs": [],
   "source": [
    "in_shp = \"us_cities.shp\"\n",
    "url = \"https://github.com/giswqs/data/raw/main/us/us_cities.zip\"\n",
    "geemap.download_file(url)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "29",
   "metadata": {},
   "outputs": [],
   "source": [
    "in_fc = geemap.shp_to_ee(in_shp)\n",
    "m.add_layer(in_fc, {}, \"Cities\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "30",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.extract_values_to_points(in_fc, dem, out_fc=\"dem.shp\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "31",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.shp_to_gdf(\"dem.shp\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "32",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.extract_values_to_points(in_fc, landsat7, \"landsat.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "33",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.csv_to_df(\"landsat.csv\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "34",
   "metadata": {},
   "source": [
    "#### Extracting pixel values along a transect"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "35",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = geemap.Map(center=[40, -100], zoom=4)\n",
    "m.add_basemap(\"TERRAIN\")\n",
    "\n",
    "image = ee.Image(\"USGS/SRTMGL1_003\")\n",
    "vis_params = {\n",
    "    \"min\": 0,\n",
    "    \"max\": 4000,\n",
    "    \"palette\": [\"006633\", \"E5FFCC\", \"662A00\", \"D8D8D8\", \"F5F5F5\"],\n",
    "}\n",
    "m.add_layer(image, vis_params, \"SRTM DEM\", True, 0.5)\n",
    "m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "36",
   "metadata": {},
   "outputs": [],
   "source": [
    "line = m.user_roi\n",
    "if line is None:\n",
    "    line = ee.Geometry.LineString(\n",
    "        [[-120.2232, 36.3148], [-118.9269, 36.7121], [-117.2022, 36.7562]]\n",
    "    )\n",
    "    m.add_layer(line, {}, \"ROI\")\n",
    "m.centerObject(line)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "37",
   "metadata": {},
   "outputs": [],
   "source": [
    "reducer = \"mean\"\n",
    "transect = geemap.extract_transect(\n",
    "    image, line, n_segments=100, reducer=reducer, to_pandas=True\n",
    ")\n",
    "transect"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "38",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.line_chart(\n",
    "    data=transect,\n",
    "    x=\"distance\",\n",
    "    y=\"mean\",\n",
    "    markers=True,\n",
    "    x_label=\"Distance (m)\",\n",
    "    y_label=\"Elevation (m)\",\n",
    "    height=400,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "39",
   "metadata": {},
   "outputs": [],
   "source": [
    "transect.to_csv(\"transect.csv\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "40",
   "metadata": {},
   "source": [
    "### Zonal statistics\n",
    "\n",
    "#### Zonal statistics with an image and a feature collection"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "41",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = geemap.Map(center=[40, -100], zoom=4)\n",
    "\n",
    "# Add NASA SRTM\n",
    "dem = ee.Image(\"USGS/SRTMGL1_003\")\n",
    "dem_vis = {\n",
    "    \"min\": 0,\n",
    "    \"max\": 4000,\n",
    "    \"palette\": [\"006633\", \"E5FFCC\", \"662A00\", \"D8D8D8\", \"F5F5F5\"],\n",
    "}\n",
    "m.add_layer(dem, dem_vis, \"SRTM DEM\")\n",
    "\n",
    "# Add 5-year Landsat TOA composite\n",
    "landsat = ee.Image(\"LANDSAT/LE7_TOA_5YEAR/1999_2003\")\n",
    "landsat_vis = {\"bands\": [\"B4\", \"B3\", \"B2\"], \"gamma\": 1.4}\n",
    "m.add_layer(landsat, landsat_vis, \"Landsat\", False)\n",
    "\n",
    "# Add US Census States\n",
    "states = ee.FeatureCollection(\"TIGER/2018/States\")\n",
    "style = {\"fillColor\": \"00000000\"}\n",
    "m.add_layer(states.style(**style), {}, \"US States\")\n",
    "m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "42",
   "metadata": {},
   "outputs": [],
   "source": [
    "out_dem_stats = \"dem_stats.csv\"\n",
    "geemap.zonal_stats(\n",
    "    dem, states, out_dem_stats, statistics_type=\"MEAN\", scale=1000, return_fc=False\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "43",
   "metadata": {},
   "outputs": [],
   "source": [
    "out_landsat_stats = \"landsat_stats.csv\"\n",
    "geemap.zonal_stats(\n",
    "    landsat,\n",
    "    states,\n",
    "    out_landsat_stats,\n",
    "    statistics_type=\"MEAN\",\n",
    "    scale=1000,\n",
    "    return_fc=False,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "44",
   "metadata": {},
   "source": [
    "#### Zonal statistics by group"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "45",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = geemap.Map(center=[40, -100], zoom=4)\n",
    "\n",
    "# Add NLCD data\n",
    "dataset = ee.Image(\"USGS/NLCD_RELEASES/2019_REL/NLCD/2019\")\n",
    "landcover = dataset.select(\"landcover\")\n",
    "m.add_layer(landcover, {}, \"NLCD 2019\")\n",
    "\n",
    "# Add US census states\n",
    "states = ee.FeatureCollection(\"TIGER/2018/States\")\n",
    "style = {\"fillColor\": \"00000000\"}\n",
    "m.add_layer(states.style(**style), {}, \"US States\")\n",
    "\n",
    "# Add NLCD legend\n",
    "m.add_legend(title=\"NLCD Land Cover\", builtin_legend=\"NLCD\")\n",
    "m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "46",
   "metadata": {},
   "outputs": [],
   "source": [
    "nlcd_stats = \"nlcd_stats.csv\"\n",
    "\n",
    "geemap.zonal_stats_by_group(\n",
    "    landcover,\n",
    "    states,\n",
    "    nlcd_stats,\n",
    "    statistics_type=\"SUM\",\n",
    "    denominator=1e6,\n",
    "    decimal_places=2,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "47",
   "metadata": {},
   "outputs": [],
   "source": [
    "nlcd_stats = \"nlcd_stats_pct.csv\"\n",
    "\n",
    "geemap.zonal_stats_by_group(\n",
    "    landcover,\n",
    "    states,\n",
    "    nlcd_stats,\n",
    "    statistics_type=\"PERCENTAGE\",\n",
    "    denominator=1e6,\n",
    "    decimal_places=2,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "48",
   "metadata": {},
   "source": [
    "#### Zonal statistics with two images"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "49",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = geemap.Map(center=[40, -100], zoom=4)\n",
    "dem = ee.Image(\"USGS/3DEP/10m\")\n",
    "vis = {\"min\": 0, \"max\": 4000, \"palette\": \"terrain\"}\n",
    "m.add_layer(dem, vis, \"DEM\")\n",
    "m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "50",
   "metadata": {},
   "outputs": [],
   "source": [
    "landcover = ee.Image(\"USGS/NLCD_RELEASES/2019_REL/NLCD/2019\").select(\"landcover\")\n",
    "m.add_layer(landcover, {}, \"NLCD 2019\")\n",
    "m.add_legend(title=\"NLCD Land Cover Classification\", builtin_legend=\"NLCD\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "51",
   "metadata": {},
   "outputs": [],
   "source": [
    "stats = geemap.image_stats_by_zone(dem, landcover, reducer=\"MEAN\")\n",
    "stats"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "52",
   "metadata": {},
   "outputs": [],
   "source": [
    "stats.to_csv(\"mean.csv\", index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "53",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.image_stats_by_zone(dem, landcover, out_csv=\"std.csv\", reducer=\"STD\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "54",
   "metadata": {},
   "source": [
    "### Map algebra"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "55",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = geemap.Map()\n",
    "\n",
    "# Load a 5-year Landsat 7 composite 1999-2003.\n",
    "landsat_1999 = ee.Image(\"LANDSAT/LE7_TOA_5YEAR/1999_2003\")\n",
    "\n",
    "# Compute NDVI.\n",
    "ndvi_1999 = (\n",
    "    landsat_1999.select(\"B4\")\n",
    "    .subtract(landsat_1999.select(\"B3\"))\n",
    "    .divide(landsat_1999.select(\"B4\").add(landsat_1999.select(\"B3\")))\n",
    ")\n",
    "\n",
    "vis = {\"min\": 0, \"max\": 1, \"palette\": \"ndvi\"}\n",
    "m.add_layer(ndvi_1999, vis, \"NDVI\")\n",
    "m.add_colorbar(vis, label=\"NDVI\")\n",
    "m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "56",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Load a Landsat 8 image.\n",
    "image = ee.Image(\"LANDSAT/LC08/C02/T1_TOA/LC08_044034_20140318\")\n",
    "\n",
    "# Compute the EVI using an expression.\n",
    "evi = image.expression(\n",
    "    \"2.5 * ((NIR - RED) / (NIR + 6 * RED - 7.5 * BLUE + 1))\",\n",
    "    {\n",
    "        \"NIR\": image.select(\"B5\"),\n",
    "        \"RED\": image.select(\"B4\"),\n",
    "        \"BLUE\": image.select(\"B2\"),\n",
    "    },\n",
    ")\n",
    "\n",
    "# Define a map centered on San Francisco Bay.\n",
    "m = geemap.Map(center=[37.4675, -122.1363], zoom=9)\n",
    "\n",
    "vis = {\"min\": 0, \"max\": 1, \"palette\": \"ndvi\"}\n",
    "m.add_layer(evi, vis, \"EVI\")\n",
    "m.add_colorbar(vis, label=\"EVI\")\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "57",
   "metadata": {},
   "source": [
    "## Exercise 1 - Zonal statistics\n",
    "\n",
    "Find out which state has the highest mean temperature in the United States on June 28, 2023. Relevant Earth Engine assets:\n",
    "\n",
    "-   [ee.FeatureCollection(\"TIGER/2018/States\")](https://developers.google.com/earth-engine/datasets/catalog/TIGER_2018_States)\n",
    "-   [ee.ImageCollection(\"NOAA/GFS0P25\")](https://developers.google.com/earth-engine/datasets/catalog/NOAA_GFS0P25)\n",
    "\n",
    "![](https://i.imgur.com/GZCHHz3.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "58",
   "metadata": {},
   "source": [
    "## Working with local geospatial data\n",
    "\n",
    "### Raster data\n",
    "\n",
    "#### Single-band raster"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "59",
   "metadata": {},
   "outputs": [],
   "source": [
    "url = \"https://github.com/giswqs/data/raw/main/raster/srtm90.tif\"\n",
    "filename = \"dem.tif\"\n",
    "geemap.download_file(url, filename)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "60",
   "metadata": {},
   "source": [
    "#### Multi-band raster"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "61",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = geemap.Map()\n",
    "m.add_raster(filename, cmap=\"terrain\", layer_name=\"DEM\")\n",
    "vis_params = {\"min\": 0, \"max\": 4000, \"palette\": \"terrain\"}\n",
    "m.add_colorbar(vis_params, label=\"Elevation (m)\")\n",
    "m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "62",
   "metadata": {},
   "outputs": [],
   "source": [
    "url = \"https://github.com/giswqs/data/raw/main/raster/cog.tif\"\n",
    "filename = \"cog.tif\"\n",
    "geemap.download_file(url, filename)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "63",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = geemap.Map()\n",
    "m.add_raster(filename, indexes=[4, 1, 2], layer_name=\"False color\")\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "64",
   "metadata": {},
   "source": [
    "### Vector data\n",
    "\n",
    "#### GeoJSON"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "65",
   "metadata": {},
   "outputs": [],
   "source": [
    "in_geojson = (\n",
    "    \"https://github.com/opengeos/datasets/releases/download/vector/cables.geojson\"\n",
    ")\n",
    "m = geemap.Map()\n",
    "m.add_geojson(in_geojson, layer_name=\"Cable lines\", info_mode=\"on_hover\")\n",
    "m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "66",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = geemap.Map()\n",
    "m.add_basemap(\"CartoDB.DarkMatter\")\n",
    "callback = lambda feat: {\"color\": feat[\"properties\"][\"color\"], \"weight\": 2}\n",
    "m.add_geojson(in_geojson, layer_name=\"Cable lines\", style_callback=callback)\n",
    "m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "67",
   "metadata": {},
   "outputs": [],
   "source": [
    "url = \"https://github.com/opengeos/datasets/releases/download/world/countries.geojson\"\n",
    "m = geemap.Map()\n",
    "m.add_geojson(\n",
    "    url, layer_name=\"Countries\", fill_colors=[\"red\", \"yellow\", \"green\", \"orange\"]\n",
    ")\n",
    "m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "68",
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "\n",
    "m = geemap.Map()\n",
    "\n",
    "\n",
    "def random_color(feature):\n",
    "    return {\n",
    "        \"color\": \"black\",\n",
    "        \"weight\": 3,\n",
    "        \"fillColor\": random.choice([\"red\", \"yellow\", \"green\", \"orange\"]),\n",
    "    }\n",
    "\n",
    "\n",
    "m.add_geojson(url, layer_name=\"Countries\", style_callback=random_color)\n",
    "m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "69",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = geemap.Map()\n",
    "\n",
    "style = {\n",
    "    \"stroke\": True,\n",
    "    \"color\": \"#0000ff\",\n",
    "    \"weight\": 2,\n",
    "    \"opacity\": 1,\n",
    "    \"fill\": True,\n",
    "    \"fillColor\": \"#0000ff\",\n",
    "    \"fillOpacity\": 0.1,\n",
    "}\n",
    "\n",
    "hover_style = {\"fillOpacity\": 0.7}\n",
    "\n",
    "m.add_geojson(url, layer_name=\"Countries\", style=style, hover_style=hover_style)\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "70",
   "metadata": {},
   "source": [
    "#### Shapefile"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "71",
   "metadata": {},
   "outputs": [],
   "source": [
    "url = \"https://github.com/opengeos/datasets/releases/download/world/countries.zip\"\n",
    "geemap.download_file(url, overwrite=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "72",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = geemap.Map()\n",
    "in_shp = \"countries.shp\"\n",
    "m.add_shp(in_shp, layer_name=\"Countries\")\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "73",
   "metadata": {},
   "source": [
    "#### GeoDataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "74",
   "metadata": {},
   "outputs": [],
   "source": [
    "import geopandas as gpd\n",
    "\n",
    "m = geemap.Map(center=[40, -100], zoom=4)\n",
    "gdf = gpd.read_file(\"countries.shp\")\n",
    "m.add_gdf(gdf, layer_name=\"Countries\")\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "75",
   "metadata": {},
   "source": [
    "#### GeoPackage"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "76",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = geemap.Map()\n",
    "data = \"https://github.com/opengeos/datasets/releases/download/world/countries.gpkg\"\n",
    "m.add_vector(data, layer_name=\"Countries\")\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "77",
   "metadata": {},
   "source": [
    "#### CSV to vector"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "78",
   "metadata": {},
   "outputs": [],
   "source": [
    "data = \"https://github.com/gee-community/geemap/blob/master/examples/data/us_cities.csv\"\n",
    "geemap.csv_to_df(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "79",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.csv_to_geojson(\n",
    "    data, \"cities.geojson\", latitude=\"latitude\", longitude=\"longitude\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "80",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.csv_to_shp(data, \"cities.shp\", latitude=\"latitude\", longitude=\"longitude\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "81",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.csv_to_vector(data, \"cities.gpkg\", latitude=\"latitude\", longitude=\"longitude\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "82",
   "metadata": {},
   "outputs": [],
   "source": [
    "gdf = geemap.csv_to_gdf(data, latitude=\"latitude\", longitude=\"longitude\")\n",
    "gdf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "83",
   "metadata": {},
   "outputs": [],
   "source": [
    "cities = (\n",
    "    \"https://github.com/gee-community/geemap/blob/master/examples/data/us_cities.csv\"\n",
    ")\n",
    "m = geemap.Map(center=[40, -100], zoom=4)\n",
    "m.add_points_from_xy(cities, x=\"longitude\", y=\"latitude\")\n",
    "m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "84",
   "metadata": {},
   "outputs": [],
   "source": [
    "regions = \"https://github.com/gee-community/geemap/blob/master/examples/data/us_regions.geojson\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "85",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = geemap.Map(center=[40, -100], zoom=4)\n",
    "m.add_geojson(regions, layer_name=\"US Regions\")\n",
    "m.add_points_from_xy(\n",
    "    cities,\n",
    "    x=\"longitude\",\n",
    "    y=\"latitude\",\n",
    "    layer_name=\"US Cities\",\n",
    "    color_column=\"region\",\n",
    "    icon_names=[\"gear\", \"map\", \"leaf\", \"globe\"],\n",
    "    spin=True,\n",
    "    add_legend=True,\n",
    ")\n",
    "m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "86",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = geemap.Map(center=[40, -100], zoom=4)\n",
    "m.add_circle_markers_from_xy(\n",
    "    data,\n",
    "    x=\"longitude\",\n",
    "    y=\"latitude\",\n",
    "    radius=8,\n",
    "    color=\"blue\",\n",
    "    fill_color=\"black\",\n",
    "    fill_opacity=0.5,\n",
    ")\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "87",
   "metadata": {},
   "source": [
    "## Accessing Cloud Optimized GeoTIFFs\n",
    "\n",
    "### COG"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "88",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = geemap.Map()\n",
    "url = \"https://opendata.digitalglobe.com/events/california-fire-2020/pre-event/2018-02-16/pine-gulch-fire20/1030010076004E00.tif\"\n",
    "m.add_cog_layer(url, name=\"Fire (pre-event)\")\n",
    "m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "89",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.cog_center(url)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "90",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.cog_bands(url)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "91",
   "metadata": {},
   "outputs": [],
   "source": [
    "url2 = \"https://opendata.digitalglobe.com/events/california-fire-2020/post-event/2020-08-14/pine-gulch-fire20/10300100AAC8DD00.tif\"\n",
    "m.add_cog_layer(url2, name=\"Fire (post-event)\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "92",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = geemap.Map(center=[39.4568, -108.5107], zoom=12)\n",
    "m.split_map(left_layer=url2, right_layer=url)\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "93",
   "metadata": {},
   "source": [
    "### SpatioTemporal Asset Catalog (STAC)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "94",
   "metadata": {},
   "outputs": [],
   "source": [
    "url = \"https://tinyurl.com/22vptbws\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "95",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.stac_bounds(url)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "96",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.stac_center(url)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "97",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.stac_bands(url)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "98",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = geemap.Map()\n",
    "m.add_stac_layer(url, bands=[\"pan\"], name=\"Panchromatic\")\n",
    "m.add_stac_layer(url, bands=[\"B3\", \"B2\", \"B1\"], name=\"False color\")\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "99",
   "metadata": {},
   "source": [
    "## Exporting Earth Engine data\n",
    "\n",
    "### Exporting images\n",
    "\n",
    "Add a Landsat image to the map."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "100",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = geemap.Map()\n",
    "\n",
    "image = ee.Image(\"LANDSAT/LC08/C02/T1_TOA/LC08_044034_20140318\").select(\n",
    "    [\"B5\", \"B4\", \"B3\"]\n",
    ")\n",
    "\n",
    "vis_params = {\"min\": 0, \"max\": 0.5, \"gamma\": [0.95, 1.1, 1]}\n",
    "\n",
    "m.center_object(image)\n",
    "m.add_layer(image, vis_params, \"Landsat\")\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "101",
   "metadata": {},
   "source": [
    "Add a rectangle to the map."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "102",
   "metadata": {},
   "outputs": [],
   "source": [
    "region = ee.Geometry.BBox(-122.5955, 37.5339, -122.0982, 37.8252)\n",
    "fc = ee.FeatureCollection(region)\n",
    "style = {\"color\": \"ffff00ff\", \"fillColor\": \"00000000\"}\n",
    "m.add_layer(fc.style(**style), {}, \"ROI\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "103",
   "metadata": {},
   "source": [
    "To local drive"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "104",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.ee_export_image(image, filename=\"landsat.tif\", scale=30, region=region)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "105",
   "metadata": {},
   "source": [
    "Check image projection."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "106",
   "metadata": {},
   "outputs": [],
   "source": [
    "projection = image.select(0).projection().getInfo()\n",
    "projection"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "107",
   "metadata": {},
   "outputs": [],
   "source": [
    "crs = projection[\"crs\"]\n",
    "crs_transform = projection[\"transform\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "108",
   "metadata": {},
   "source": [
    "Specify region, crs, and crs_transform."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "109",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.ee_export_image(\n",
    "    image,\n",
    "    filename=\"landsat_crs.tif\",\n",
    "    crs=crs,\n",
    "    crs_transform=crs_transform,\n",
    "    region=region,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "110",
   "metadata": {},
   "source": [
    "To Google Drive"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "111",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.ee_export_image_to_drive(\n",
    "    image, description=\"landsat\", folder=\"export\", region=region, scale=30\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "112",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.download_ee_image(image, \"landsat.tif\", scale=90)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "113",
   "metadata": {},
   "source": [
    "### Exporting image collections"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "114",
   "metadata": {},
   "outputs": [],
   "source": [
    "point = ee.Geometry.Point(-99.2222, 46.7816)\n",
    "collection = (\n",
    "    ee.ImageCollection(\"USDA/NAIP/DOQQ\")\n",
    "    .filterBounds(point)\n",
    "    .filterDate(\"2008-01-01\", \"2018-01-01\")\n",
    "    .filter(ee.Filter.listContains(\"system:band_names\", \"N\"))\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "115",
   "metadata": {},
   "outputs": [],
   "source": [
    "collection.aggregate_array(\"system:index\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "116",
   "metadata": {},
   "source": [
    "To local drive"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "117",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.ee_export_image_collection(collection, out_dir=\".\", scale=10)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "118",
   "metadata": {},
   "source": [
    "To Google Drive"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "119",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.ee_export_image_collection_to_drive(collection, folder=\"export\", scale=10)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "120",
   "metadata": {},
   "source": [
    "### Exporting feature collections"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "121",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = geemap.Map()\n",
    "states = ee.FeatureCollection(\"TIGER/2018/States\")\n",
    "fc = states.filter(ee.Filter.eq(\"NAME\", \"Alaska\"))\n",
    "m.add_layer(fc, {}, \"Alaska\")\n",
    "m.center_object(fc, 4)\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "122",
   "metadata": {},
   "source": [
    "To local drive"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "123",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.ee_to_shp(fc, filename=\"Alaska.shp\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "124",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.ee_export_vector(fc, filename=\"Alaska.shp\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "125",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.ee_to_geojson(fc, filename=\"Alaska.geojson\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "126",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.ee_to_csv(fc, filename=\"Alaska.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "127",
   "metadata": {},
   "outputs": [],
   "source": [
    "gdf = geemap.ee_to_gdf(fc)\n",
    "gdf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "128",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = geemap.ee_to_df(fc)\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "129",
   "metadata": {},
   "source": [
    "To Google Drive"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "130",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.ee_export_vector_to_drive(\n",
    "    fc, description=\"Alaska\", fileFormat=\"SHP\", folder=\"export\"\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "131",
   "metadata": {},
   "source": [
    "## Creating timelapse animations\n",
    "\n",
    "### Landsat timelapse"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "132",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = geemap.Map(center=[64.838721, -147.763366], zoom=11)\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "133",
   "metadata": {},
   "source": [
    "Pan and zoom the map to an area of interest. Use the drawing tools to draw a rectangle on the map. If no rectangle is drawn, the default rectangle shown below will be used."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "134",
   "metadata": {},
   "outputs": [],
   "source": [
    "roi = m.user_roi\n",
    "if roi is None:\n",
    "    roi = ee.Geometry.BBox(-147.9701, 64.7733, -147.5849, 64.8717)\n",
    "    m.add_layer(roi)\n",
    "    m.center_object(roi)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "135",
   "metadata": {},
   "outputs": [],
   "source": [
    "timelapse = geemap.landsat_timelapse(\n",
    "    roi,\n",
    "    out_gif=\"Fairbanks.gif\",\n",
    "    start_year=2000,\n",
    "    end_year=2023,\n",
    "    start_date=\"06-01\",\n",
    "    end_date=\"09-01\",\n",
    "    bands=[\"SWIR1\", \"NIR\", \"Red\"],\n",
    "    frames_per_second=5,\n",
    "    title=\"Landsat Timelapse\",\n",
    "    progress_bar_color=\"blue\",\n",
    "    mp4=True,\n",
    ")\n",
    "geemap.show_image(timelapse)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "136",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = geemap.Map(center=[64.838721, -147.763366], zoom=11)\n",
    "m.add_gui(\"timelapse\")\n",
    "m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "137",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = geemap.Map()\n",
    "roi = ee.Geometry.BBox(-115.5541, 35.8044, -113.9035, 36.5581)\n",
    "m.add_layer(roi)\n",
    "m.center_object(roi)\n",
    "m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "138",
   "metadata": {},
   "outputs": [],
   "source": [
    "timelapse = geemap.landsat_timelapse(\n",
    "    roi,\n",
    "    out_gif=\"las_vegas.gif\",\n",
    "    start_year=1984,\n",
    "    end_year=2023,\n",
    "    bands=[\"NIR\", \"Red\", \"Green\"],\n",
    "    frames_per_second=5,\n",
    "    title=\"Las Vegas, NV\",\n",
    "    font_color=\"blue\",\n",
    ")\n",
    "geemap.show_image(timelapse)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "139",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = geemap.Map()\n",
    "roi = ee.Geometry.BBox(113.8252, 22.1988, 114.0851, 22.3497)\n",
    "m.add_layer(roi)\n",
    "m.center_object(roi)\n",
    "m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "140",
   "metadata": {},
   "outputs": [],
   "source": [
    "timelapse = geemap.landsat_timelapse(\n",
    "    roi,\n",
    "    out_gif=\"hong_kong.gif\",\n",
    "    start_year=1990,\n",
    "    end_year=2022,\n",
    "    start_date=\"01-01\",\n",
    "    end_date=\"12-31\",\n",
    "    bands=[\"SWIR1\", \"NIR\", \"Red\"],\n",
    "    frames_per_second=3,\n",
    "    title=\"Hong Kong\",\n",
    ")\n",
    "geemap.show_image(timelapse)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "141",
   "metadata": {},
   "source": [
    "### Sentinel-2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "142",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = geemap.Map(center=[64.838721, -147.763366], zoom=11)\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "143",
   "metadata": {},
   "source": [
    "Pan and zoom the map to an area of interest. Use the drawing tools to draw a rectangle on the map. If no rectangle is drawn, the default rectangle shown below will be used."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "144",
   "metadata": {},
   "outputs": [],
   "source": [
    "roi = m.user_roi\n",
    "if roi is None:\n",
    "    roi = ee.Geometry.BBox(-147.9701, 64.7733, -147.5849, 64.8717)\n",
    "    m.add_layer(roi)\n",
    "    m.center_object(roi)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "145",
   "metadata": {},
   "outputs": [],
   "source": [
    "timelapse = geemap.sentinel2_timelapse(\n",
    "    roi,\n",
    "    out_gif=\"sentinel2.gif\",\n",
    "    start_year=2017,\n",
    "    end_year=2023,\n",
    "    start_date=\"06-01\",\n",
    "    end_date=\"09-01\",\n",
    "    frequency=\"year\",\n",
    "    bands=[\"SWIR1\", \"NIR\", \"Red\"],\n",
    "    frames_per_second=3,\n",
    "    title=\"Sentinel-2 Timelapse\",\n",
    ")\n",
    "geemap.show_image(timelapse)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "146",
   "metadata": {},
   "source": [
    "### MODIS\n",
    "\n",
    "MODIS vegetation indices"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "147",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = geemap.Map()\n",
    "m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "148",
   "metadata": {},
   "outputs": [],
   "source": [
    "roi = m.user_roi\n",
    "if roi is None:\n",
    "    roi = ee.Geometry.BBox(-18.6983, -36.1630, 52.2293, 38.1446)\n",
    "    m.add_layer(roi)\n",
    "    m.center_object(roi)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "149",
   "metadata": {},
   "outputs": [],
   "source": [
    "timelapse = geemap.modis_ndvi_timelapse(\n",
    "    roi,\n",
    "    out_gif=\"ndvi.gif\",\n",
    "    data=\"Terra\",\n",
    "    band=\"NDVI\",\n",
    "    start_date=\"2000-01-01\",\n",
    "    end_date=\"2022-12-31\",\n",
    "    frames_per_second=3,\n",
    "    title=\"MODIS NDVI Timelapse\",\n",
    "    overlay_data=\"countries\",\n",
    ")\n",
    "geemap.show_image(timelapse)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "150",
   "metadata": {},
   "source": [
    "MODIS temperature"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "151",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = geemap.Map()\n",
    "m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "152",
   "metadata": {},
   "outputs": [],
   "source": [
    "roi = m.user_roi\n",
    "if roi is None:\n",
    "    roi = ee.Geometry.BBox(-171.21, -57.13, 177.53, 79.99)\n",
    "    m.add_layer(roi)\n",
    "    m.center_object(roi)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "153",
   "metadata": {},
   "outputs": [],
   "source": [
    "timelapse = geemap.modis_ocean_color_timelapse(\n",
    "    satellite=\"Aqua\",\n",
    "    start_date=\"2018-01-01\",\n",
    "    end_date=\"2020-12-31\",\n",
    "    roi=roi,\n",
    "    frequency=\"month\",\n",
    "    out_gif=\"temperature.gif\",\n",
    "    overlay_data=\"continents\",\n",
    "    overlay_color=\"yellow\",\n",
    "    overlay_opacity=0.5,\n",
    ")\n",
    "geemap.show_image(timelapse)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "154",
   "metadata": {},
   "source": [
    "### GOES"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "155",
   "metadata": {},
   "outputs": [],
   "source": [
    "roi = ee.Geometry.BBox(167.1898, -28.5757, 202.6258, -12.4411)\n",
    "start_date = \"2022-01-15T03:00:00\"\n",
    "end_date = \"2022-01-15T07:00:00\"\n",
    "data = \"GOES-17\"\n",
    "scan = \"full_disk\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "156",
   "metadata": {},
   "outputs": [],
   "source": [
    "timelapse = geemap.goes_timelapse(\n",
    "    roi, \"goes.gif\", start_date, end_date, data, scan, framesPerSecond=5\n",
    ")\n",
    "geemap.show_image(timelapse)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "157",
   "metadata": {},
   "outputs": [],
   "source": [
    "roi = ee.Geometry.BBox(-159.5954, 24.5178, -114.2438, 60.4088)\n",
    "start_date = \"2021-10-24T14:00:00\"\n",
    "end_date = \"2021-10-25T01:00:00\"\n",
    "data = \"GOES-17\"\n",
    "scan = \"full_disk\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "158",
   "metadata": {},
   "outputs": [],
   "source": [
    "timelapse = geemap.goes_timelapse(\n",
    "    roi, \"hurricane.gif\", start_date, end_date, data, scan, framesPerSecond=5\n",
    ")\n",
    "geemap.show_image(timelapse)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "159",
   "metadata": {},
   "outputs": [],
   "source": [
    "roi = ee.Geometry.BBox(-121.0034, 36.8488, -117.9052, 39.0490)\n",
    "start_date = \"2020-09-05T15:00:00\"\n",
    "end_date = \"2020-09-06T02:00:00\"\n",
    "data = \"GOES-17\"\n",
    "scan = \"full_disk\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "160",
   "metadata": {},
   "outputs": [],
   "source": [
    "timelapse = geemap.goes_fire_timelapse(\n",
    "    roi, \"fire.gif\", start_date, end_date, data, scan, framesPerSecond=5\n",
    ")\n",
    "geemap.show_image(timelapse)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "161",
   "metadata": {},
   "source": [
    "## Exercise 2 - Creating timelapse animations\n",
    "\n",
    "Use the geemap timelapse GUI to create a timelapse animation for any location of your choice. Share the timelapse on social media and use the hashtagd such as #EarthEngine and #geemap. See [this](https://i.imgur.com/YaCHvKC.gif) example.\n",
    "\n",
    "![](https://i.imgur.com/ohrXeFC.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "162",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "163",
   "metadata": {},
   "source": [
    "## Time series analysis\n",
    "\n",
    "### Visualizing forest cover\n",
    "\n",
    "We will use the [Hansen Global Forest Change v1.10 (2000-2022) dataset](https://developers.google.com/earth-engine/datasets/catalog/UMD_hansen_global_forest_change_2022_v1_10)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "164",
   "metadata": {},
   "outputs": [],
   "source": [
    "dataset = ee.Image(\"UMD/hansen/global_forest_change_2022_v1_10\")\n",
    "dataset.bandNames()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "165",
   "metadata": {},
   "source": [
    "Select the imagery for 2000."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "166",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = geemap.Map()\n",
    "first_bands = [\"first_b50\", \"first_b40\", \"first_b30\"]\n",
    "first_image = dataset.select(first_bands)\n",
    "m.add_layer(first_image, {\"bands\": first_bands, \"gamma\": 1.5}, \"Landsat 2000\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "167",
   "metadata": {},
   "source": [
    "Select the imagery for 2022."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "168",
   "metadata": {},
   "outputs": [],
   "source": [
    "last_bands = [\"last_b50\", \"last_b40\", \"last_b30\"]\n",
    "last_image = dataset.select(last_bands)\n",
    "m.add_layer(last_image, {\"bands\": last_bands, \"gamma\": 1.5}, \"Landsat 2022\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "169",
   "metadata": {},
   "source": [
    "Select the tree cover imagery for 2000."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "170",
   "metadata": {},
   "outputs": [],
   "source": [
    "treecover = dataset.select([\"treecover2000\"])\n",
    "treeCoverVisParam = {\"min\": 0, \"max\": 100, \"palette\": [\"black\", \"green\"]}\n",
    "name = \"Tree cover (%)\"\n",
    "m.add_layer(treecover, treeCoverVisParam, name)\n",
    "m.add_colorbar(treeCoverVisParam, label=name, layer_name=name)\n",
    "m.add(\"layer_manager\")\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "171",
   "metadata": {},
   "source": [
    "Extract tree cover 2000 by using the threshold of 10%."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "172",
   "metadata": {},
   "outputs": [],
   "source": [
    "threshold = 10\n",
    "treecover_bin = treecover.gte(threshold).selfMask()\n",
    "treeVisParam = {\"palette\": [\"green\"]}\n",
    "m.add_layer(treecover_bin, treeVisParam, \"Tree cover bin\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "173",
   "metadata": {},
   "source": [
    "### Visualizing forest gain and loss\n",
    "\n",
    "Visualize forest loss."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "174",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = geemap.Map(center=[64.864983, -147.840441], zoom=4)\n",
    "m.add_basemap(\"Esri.WorldImagery\")\n",
    "treeloss_year = dataset.select([\"lossyear\"])\n",
    "treeLossVisParam = {\"min\": 0, \"max\": 22, \"palette\": [\"yellow\", \"red\"]}\n",
    "layer_name = \"Tree loss year\"\n",
    "m.add_layer(treeloss_year, treeLossVisParam, layer_name)\n",
    "m.add_colorbar(treeLossVisParam, label=layer_name, layer_name=layer_name)\n",
    "m.add(\"layer_manager\")\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "175",
   "metadata": {},
   "source": [
    "Compare forest loss and gain."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "176",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = geemap.Map(center=[64.864983, -147.840441], zoom=4)\n",
    "m.add_basemap(\"Esri.WorldImagery\")\n",
    "treeloss = dataset.select([\"loss\"]).selfMask()\n",
    "treegain = dataset.select([\"gain\"]).selfMask()\n",
    "m.add_layer(treeloss, {\"palette\": \"red\"}, \"Tree loss\")\n",
    "m.add_layer(treegain, {\"palette\": \"yellow\"}, \"Tree gain\")\n",
    "m.add(\"layer_manager\")\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "177",
   "metadata": {},
   "source": [
    "### Calculating forest cover change\n",
    "\n",
    "Compute zonal statistics to find out which county in Alaska has the largest forest area in 2000.\n",
    "\n",
    "Add a county boundary layer to the map."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "178",
   "metadata": {},
   "outputs": [],
   "source": [
    "counties = ee.FeatureCollection(\"TIGER/2018/Counties\").filter(\n",
    "    ee.Filter.eq(\"STATEFP\", \"02\")\n",
    ")\n",
    "df = geemap.ee_to_df(counties)\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "179",
   "metadata": {},
   "outputs": [],
   "source": [
    "style = {\"color\": \"0000FFFF\", \"fillColor\": \"00000000\"}\n",
    "m.add_layer(counties, {}, \"Counties Vector\", False)\n",
    "m.add_layer(counties.style(**style), {}, \"Counties Raster\")\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "180",
   "metadata": {},
   "source": [
    "Compute zonal statistics by county."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "181",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.zonal_stats(\n",
    "    treecover_bin,\n",
    "    counties,\n",
    "    \"forest_cover.csv\",\n",
    "    stat_type=\"SUM\",\n",
    "    denominator=1e6,\n",
    "    scale=300,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "182",
   "metadata": {},
   "source": [
    "Create a pie chart to visualize the forest area by county."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "183",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.pie_chart(\n",
    "    \"forest_cover.csv\", names=\"NAME\", values=\"sum\", max_rows=20, height=400\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "184",
   "metadata": {},
   "source": [
    "Create a bar chart to visualize the forest area by county."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "185",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.bar_chart(\n",
    "    \"forest_cover.csv\",\n",
    "    x=\"NAME\",\n",
    "    y=\"sum\",\n",
    "    max_rows=20,\n",
    "    x_label=\"County\",\n",
    "    y_label=\"Forest area (km2)\",\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "186",
   "metadata": {},
   "source": [
    "Calculate the forest loss area by county."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "187",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.zonal_stats(\n",
    "    treeloss.gt(0),\n",
    "    counties,\n",
    "    \"treeloss.csv\",\n",
    "    stat_type=\"SUM\",\n",
    "    denominator=1e6,\n",
    "    scale=300,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "188",
   "metadata": {},
   "source": [
    "Create a bar chart to visualize the forest loss area by county."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "189",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.pie_chart(\"treeloss.csv\", names=\"NAME\", values=\"sum\", max_rows=20, height=600)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "190",
   "metadata": {},
   "source": [
    "Create a bar chart to visualize the forest loss area by county."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "191",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.bar_chart(\n",
    "    \"treeloss.csv\",\n",
    "    x=\"NAME\",\n",
    "    y=\"sum\",\n",
    "    max_rows=20,\n",
    "    x_label=\"County\",\n",
    "    y_label=\"Forest loss area (km2)\",\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "192",
   "metadata": {},
   "source": [
    "## Exercise 3 - Analyzing forest cover gain and loss\n",
    "\n",
    "Find out which US state has the largest forest gain and loss between 2000 and 2022. Create pie charts and bar charts to show the results. Relevant Earth Engine assets:\n",
    "\n",
    "-   [ee.FeatureCollection(\"TIGER/2018/States\")](https://developers.google.com/earth-engine/datasets/catalog/TIGER_2018_States)\n",
    "-   [ee.Image(\"UMD/hansen/global_forest_change_2022_v1_10\")](https://developers.google.com/earth-engine/datasets/catalog/UMD_hansen_global_forest_change_2022_v1_10)\n",
    "\n",
    "![](https://i.imgur.com/NQ4UUnj.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "193",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
